﻿namespace ClientWF
{
    using System.Threading;
    public class Presenter : IPresenter
    {
        public IModel model { get; set; }
        public IView view { get; set; }
        public int ServerProt { get; private set; }
        public string ServerAddress { get;private set; }
        string connString;
        string nickname;
        Thread conCheck;
        bool status;

        public Presenter()
            : this(8811, "localhost")
        {
        }
        public Presenter(int port, string address)
        {
            this.ServerAddress = address;
            this.ServerProt = port;
            connString = "http://" + ServerAddress + ":" + ServerProt + "/";
        }
        public void send(string message)
        {
            string[] tmp = message.Split('^');
            if (tmp[0] =="Login")
            {
                status = true;
                nickname = tmp[1];
                conCheck = new Thread(conChecking);
                conCheck.Start();
            };
            if (message == "disconnect") 
            { 
                conCheck.Abort(); 
            };
            status = true;
            answer(model.recieve(connString, message));
        }
        private void conChecking()
        {
            while (true)
            {
                Thread.Sleep(2000);
                if (!status)
                {
                    answer(model.recieve(connString, "Check^" + nickname));
                };
            }
        }
        public IGame initGame(string msg)
        {
            return Factory.gi(msg);
        }
        private void answer(string message)
        {

            string[] split = message.Split('^');
            switch (split[0])
            {
                case "Wrong move":break;
                case "Invited": view.show(split[1]+'^'+split[2], "Invited"); break;
                case "Accepted": view.show("0,0,0,0,0,0,0,0,0^Your Move^Y","ok"); break;
                case "Declined": view.show(split[0], "Declined"); break;
                case "ok":
                    if (split.Length > 1)
                    {
                        switch(split[1]){
                            case "Victory": view.show("Win", "Endgame"); break;
                            case "Draw": view.show("Draw", "Endgame"); break;
                            case "Lost": view.show("Lost", "Endgame"); break;
                            default: 
                                view.show(split[1] + "^" + split[2], "ok"); break;
                        }
                    }
                    else
                    {
                        
                        view.show("0,0,0,0,0,0,0,0,0^Opponent Move^N", "ok");
                    }
                    break;               
                default:
                    view.show(message,"command");
                    break;
            }
            
            status = false;
        }

    }
}